package com.iman.Confucius.service.impl.share;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.Confucius.service.share.StudyResourceService;
import com.iman.Confucius.vo.share.ShareExpertVo;
import com.iman.Confucius.vo.share.StudyResourceVo;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;

@Service
public class StudyResourceServiceImpl implements StudyResourceService {
	@Inject
	TemplateQLService templateQLService;
	
	@Override
	public Page<StudyResourceVo> newestResourceList(int pageNum, int pageSize) {
		int pageIndex = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("resource-newest-list", null, pageIndex, pageSize, StudyResourceVo.class);
	}

	@Override
	public Page<StudyResourceVo> hotestResourceList(int pageNum, int pageSize) {
		int pageIndex = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("resource-hotest-list", null, pageIndex, pageSize, StudyResourceVo.class);
	}

	@Override
	public Page<StudyResourceVo> downRankResourceList(int pageNum, int pageSize) {
		int pageIndex = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("resource-downRank-list", null, pageIndex, pageSize, StudyResourceVo.class);
	}

	@Override
	public Page<ShareExpertVo> shareExpertList(int pageNum, int pageSize) {
		int pageIndex = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("resource-shareExpert-list", null, pageIndex, pageSize, ShareExpertVo.class);
	}
	
	@Override
	public Page<StudyResourceVo> resourceListByTagId(int pageNum,int pageSize,Map<String, Object> model)
	{
		int pageIndex = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("resource-listbytag-list", model, pageIndex, pageSize, StudyResourceVo.class);
	}

	@Override
	public StudyResourceVo resourceDetail(long file_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("file_id", file_id);
		return templateQLService.findObjectByQuery("resource-detail-byId", params, StudyResourceVo.class);
	}

	@Override
	public int incrViewCnt(long resource_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);		
		return templateQLService.executeSQLUpdate("resource-incr-viewcnt", params);
	}

	@Override
	public List<ShareExpertVo> downUserList(long resource_id, int index,
			int limit) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);
		params.put("index", index);
		params.put("limit", limit);
		return templateQLService.findBySQLBeanQuery("resource-down-userlist", params, ShareExpertVo.class);
	}

	@Override
	public StudyResourceVo resourceSimpleDetail(long resource_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);
		return templateQLService.findObjectByQuery("resource-detail-info", params, StudyResourceVo.class);
	}
	@Override
	public ShareExpertVo userIntegral(long user_id){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("user_id", user_id);
		return templateQLService.findObjectByQuery("resource-user-integral", params, ShareExpertVo.class);
	}

	@Override
	public ShareExpertVo commentUserHead(long user_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("user_id", user_id);
		return templateQLService.findObjectByQuery("resource-comment-user-info", params, ShareExpertVo.class);
	}

	@Override
	public int incrDownCnt(long resource_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);		
		return templateQLService.executeSQLUpdate("resource-incr-downcnt", params);
	}

	@Override
	public int recordDown(long resource_id,long user_id,int integral) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);	
		params.put("user_id", user_id);	
		params.put("integral", integral);
		params.put("download_time", new Date());
		return templateQLService.executeSQLUpdate("insert-down-history", params);
	}

	@Override
	public int reduceDownIntegral(long user_id, int integral) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("integral", integral);	
		params.put("user_id", user_id);	
		return templateQLService.executeSQLUpdate("resource-incr-downcnt", params);
	}
}
